Page({
  data: {
    proObj: {},
    sObj: {},
    num: 0,
    addflage: 1,
    favoriteStatus: "收藏商品",
  },
  onLoad: function (options) {
    if (options.skuid != undefined) {
      this.setData({
        skuId: options.skuid
      })
    } else {
      wx.clearStorageSync()
      getApp().data.entryMode = "share";
      var sharerSku;
      if (options.sku == "" || options.sku == undefined) {
        sharerSku = getApp().data.shareSku;
      } else {
        getApp().data.shareSku = options.sku;
        sharerSku = options.sku;
      }

      this.setData({
        skuId: sharerSku
      })
    }
    console.log(options)
  },
  onShow: function () {
    var entryMode = getApp().data.entryMode;
    if (entryMode == "share") {
      this.setData({
        entryMod: getApp().data.entryMode
      })
      this.haveUserid();
    } else {
      this.getDetailsInfo();
      getApp().getCartNum(this);
    }

  },
  //分享方式进入
  haveUserid() {
    var userid = wx.getStorageSync('userid');
    var srcpage = "pDetails&sku=" + this.data.skuId;
    getApp().rueLogin(this.getShareGoodInfo, this, srcpage);

  },
  getShareGoodInfo() {
    var me = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        var through = res.longitude;
        var weft = res.latitude;
        var url = getApp().data.url + 'product/getProductNew';
        var body = {
          through: through,
          weft: weft,
          sku: me.data.skuId
        };
        console.log(body)
        console.log(wx.getStorageSync('userid'))

        getApp().wxAjax(me.getShareGoodInfoSug, url, body);
      }
    })

  },
  getShareGoodInfoSug(res) {
    var resObj = res.data;
    console.log(resObj)
    if (resObj.header.res_code == 0) {
      var pDetailsObj = resObj.body;
      var storeInfoObj = resObj.body.re_tposstore;
      //设置有效地址
      console.log(resObj.body.is_valid_address);
      getApp().data.is_valid_address = resObj.body.is_valid_address;
      console.log(getApp().data.is_valid_address)
      //缓存门店信息
      wx.setStorageSync('supermarket_id', storeInfoObj.sstoreid);
      wx.setStorageSync('seller_id', storeInfoObj.scorpid);
      wx.setStorageSync('supername', storeInfoObj.sstorename);
      wx.setStorageSync('superaddress', storeInfoObj.sstoreaddr);
      getApp().getCartNum(this);
      //地址信息
      var addressInfoObj = resObj.body.re_address_vo;
      console.log(addressInfoObj)
      if (addressInfoObj != undefined) {
        wx.setStorageSync('addressId', addressInfoObj.address_id)
      }
      this.setData({
        proObj: pDetailsObj,
        sObj: storeInfoObj
      })
      if (pDetailsObj.seckill == true) {
        this.secKill();
      }
      if (pDetailsObj.is_focus) {
        this.setData({
          favoriteStatus: "已收藏"
        })
      } else {
        this.setData({
          favoriteStatus: "收藏商品"
        })
      }
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  //正常方式进入
  getDetailsInfo() {
    var url = getApp().data.url + 'product/getProduct';
    var body = {
      supermarket_id: wx.getStorageSync('supermarket_id'),
      sku: this.data.skuId
    };
    getApp().wxAjax(this.splitDetails, url, body);
  },
  splitDetails(res) {
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      var pDetailsObj = resObj.body;
      this.setData({
        proObj: pDetailsObj
      })
      if (pDetailsObj.is_focus) {
        this.setData({
          favoriteStatus: "已收藏"
        })
      } else {
        this.setData({
          favoriteStatus: "收藏商品"
        })
      }

      console.log(pDetailsObj)
      //秒杀
      if (pDetailsObj.seckill == true){
        this.secKill();
      }else{
        this.setData({
          secKillFlage: 2
        })
      }
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  //秒杀相关
  secKill(){
    this.setData({
      secKillFlage:1
    })
    var endTime = this.data.proObj.endtime
    var nowTime = this.data.proObj.now;
    console.log(endTime)
    getApp().getActiveEndTime(this, nowTime,endTime,this.seckillover);
  },
  seckillover(){
    this.getDetailsInfo();
    getApp().getCartNum(this);
  },
  //收藏商品
  tofollow() {
    if (this.data.favoriteStatus == '收藏商品')
      this.tofocus()
    else
      this.tounfocus()
  },
  tofocus() {
    var url = getApp().data.url + 'product/addAttention';
    var body = {
      supermarket_id: wx.getStorageSync('supermarket_id'),
      product_id: this.data.proObj.prod_id
    };
    getApp().wxAjax(this.echofocus, url, body);
  },
  echofocus(res) {
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      this.setData({
        favoriteStatus: "已收藏"
      })
      wx.showToast({
        title: '收藏成功',
        icon: 'none'
      })
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  tounfocus() {
    var url = getApp().data.url + 'product/cancelAttention';
    var body = {
      supermarket_id: wx.getStorageSync('supermarket_id'),
      product_id: this.data.proObj.prod_id
    };
    getApp().wxAjax(this.echounfocus, url, body);
  },
  echounfocus(res) {
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      this.setData({
        favoriteStatus: "收藏商品"
      })
      wx.showToast({
        title: '已取消收藏',
        icon: 'none'
      })
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  addPro() {
    var me = this;
    var url = getApp().data.url + 'shoppingcart/addShoppingCart';
    var body = {
      quantity: '1',
      supermarket_id: wx.getStorageSync('supermarket_id'),
      sku: this.data.proObj.sku
    };
    if (this.data.addflage == 1) {
      this.setData({
        addflage: 2
      })
      var addressid = wx.getStorageSync('addressId');
      var isValidAddress = getApp().data.is_valid_address;
      if (isValidAddress == "false" && getApp().data.entryMode == "share") {
        wx.showModal({
          title: '亲，附近暂无可配送门店',
          confirmText: "确认",
          cancelColor: "#999",
          content: '换个地址试试吧~',
          success: function (res) {
            if (res.confirm) {
              me.setData({
                addflage: 1
              })
              me.toAddAddress();
            } else if (res.cancel) {
              me.setData({
                addflage: 1
              })
            }
          }
        })
      } else {
        getApp().wxAjax(this.addProSug, url, body);
      }

    }

  },
  addProSug(res) {
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      this.setData({
        addflage: 1
      })
      wx.showToast({
        title: '添加成功',
      })
      getApp().getCartNum(this);
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  tocart() {
    var me = this;
    var addressid = wx.getStorageSync('addressId');
    var isValidAddress = getApp().data.is_valid_address;
    if (isValidAddress == "false" && getApp().data.entryMode == "share") {
      wx.showModal({
        title: '亲，附近暂无可配送门店',
        confirmText: "确认",
        cancelColor: "#999",
        content: '换个地址试试吧~~',
        success: function (res) {
          if (res.confirm) {
            me.toAddAddress();
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.navigateTo({
        url: '../cart/cart'
      })
    }
  },
  toAddAddress() {
    wx.navigateTo({
      url: '../addAddress/addAddress?pageFrom=corder'
    })
  },
  toIndex() {
    wx.redirectTo({
      url: '../eIndex/eIndex'
    })
  }
})